#include "lvgl.h"
#include "img_def.h"
#include "lv_common_function.h"

static lv_timer_t *refresh_timer=NULL;
static uint8_t background_style = 0;

LV_ATTRIBUTE_LARGE_CONST static const lv_img_dsc_t* panel_bg1_src[7] =
{

IMG_BGBLACK_1,
IMG_BGBLACK_2,
IMG_BGBLACK_3,
IMG_BGBLACK_4,
IMG_BGBLACK_5,
IMG_BGBLACK_6,
IMG_BGBLACK_7,
//IMG_CENTRE_1,
//IMG_CENTRE_2,


};



LV_ATTRIBUTE_LARGE_CONST static const lv_img_dsc_t* panel_bg2_src[3] =
{
     IMG_SPEED_1,
     IMG_SPEED_2,
     IMG_SPEED_3,
};

LV_ATTRIBUTE_LARGE_CONST static const lv_img_dsc_t* panel_bg3_src[2] =
{
    IMG_CENTRE_1,
    IMG_CENTRE_2,

};




static uint16_t oil_times=0;
static uint16_t oil_speed=17;
static uint16_t speed_times=0;
static int speed_angle=2400;
extern volatile uint8_t lock_timer_flag;

void fr_lv_instrument_panel_func(lv_obj_t *parent, lv_point_t *top);

static void refresh_timer_cb(lv_timer_t *t)
{


    lv_obj_t *parent = t->user_data;
    if(!lv_obj_is_valid(parent))
		return;

    lock_timer_flag = 1;

	lv_obj_t * pointer_img = lv_obj_get_child(parent, 2);
    lv_obj_t * arc_oil = lv_obj_get_child(parent,3);

    lv_obj_t * speed_lab = lv_obj_get_child(parent, lv_obj_get_child_cnt(parent)-2);

    lv_img_set_angle(pointer_img,speed_angle);
    lv_arc_set_value(arc_oil,oil_speed);
//  lv_img_set_angle(oil_blue,oil_speed);
    lv_label_set_text_fmt(speed_lab,"%d",(speed_angle-2400)/200);

    if(oil_times==0)
    {
        oil_speed-=1;
        if(oil_speed<1)
        {
            oil_times=1;
        }
    }
    else
    {
         oil_speed+=1;
         if(oil_speed>16)
            {
            oil_times=0;
         }
    }

    if(speed_times==0)
    {
        speed_angle+=100;
        if(speed_angle>4000)
        {
            //speed_angle=2400;
            speed_times=1;

        }
    }
    else
    {
        speed_angle-=100;
        if(speed_angle<2500)
        {
            speed_times=0;
        }
    }

}
static void page_del_cb(lv_event_t *e)
{
	if(refresh_timer!=NULL)
	{
		lv_timer_del(refresh_timer);
        refresh_timer = NULL;
        lock_timer_flag = 0;
	}
}
static void page_event_handler(lv_event_t *e)
{
	lv_event_code_t event = lv_event_get_code(e);
	lv_obj_t *obj = lv_event_get_target(e);
	if(!lv_obj_is_valid(obj))
        return;
	if(event == LV_EVENT_DELETE)
	{

	}else if(event == LV_EVENT_KEY)
	{
		uint32_t *key = lv_event_get_param(e);
        switch(*key)
        {
        case KEY1_CODE:
				user_jump_page(fr_lv_instrument_panel_func);
             break;
        case KEY3_CODE:
				 
             break;
        case KEY5_CODE: //return
				user_jump_page(fr_list_page);
            break;
        }
		printf("key_code %d \n", *key);
	}
}
void fr_lv_instrument_panel_km_func(lv_obj_t *parent, lv_point_t *top)
{
    //start_angle = atan2(center_y - 143, 199 - center_x) * 180 / M_PI;
    // end_angle = atan2(center_y - 143, 284 - center_x) * 180 / M_PI;
    if(!lv_obj_is_valid(parent))
    return;
    UI_PARENT_INIT(parent);


    lv_obj_t* panel_bg_img = lv_img_big_create(parent,panel_bg1_src,0,0,7,0);//0
    lv_obj_align(panel_bg_img,LV_ALIGN_TOP_LEFT,13,0);

    lv_obj_t* panel_bg_img_1 = lv_img_big_create(parent,panel_bg2_src,0,0,3,0);//1
//    lv_obj_align(panel_bg_img_1,LV_ALIGN_TOP_LEFT,75,27);
    lv_obj_align(panel_bg_img_1,LV_ALIGN_CENTER,0,0);

    lv_obj_t *pointer_img_test = lv_img_create(parent);//2
    lv_img_set_src(pointer_img_test,IMG_POINTER);

    lv_img_set_pivot(pointer_img_test, 57, 57);

    lv_obj_align(pointer_img_test,LV_ALIGN_CENTER,0,0);
//    lv_obj_align(pointer_img_test,LV_ALIGN_TOP_LEFT,138,88);
    lv_img_set_zoom(pointer_img_test, 500);


    lv_obj_t * arc_oil =lv_arc_create(parent);//-5
    lv_obj_align(arc_oil,LV_ALIGN_TOP_LEFT,152,37);
    lv_obj_set_size(arc_oil,207,207);

    lv_arc_set_value(arc_oil,17);
    lv_arc_set_range(arc_oil,0,17);
    lv_arc_set_angles(arc_oil, 0, 49);
    lv_arc_set_bg_angles(arc_oil, 0, 49);

    lv_arc_set_mode(arc_oil,LV_ARC_MODE_REVERSE);

    lv_obj_remove_style(arc_oil,NULL,LV_PART_KNOB);
    lv_obj_set_style_arc_color(arc_oil, lv_color_hex(0x000000), LV_PART_MAIN );
    lv_obj_set_style_arc_color(arc_oil, lv_color_hex(0x3e6d86), LV_PART_INDICATOR );
    lv_obj_set_style_arc_rounded(arc_oil,0,LV_PART_MAIN);
    lv_obj_set_style_arc_rounded(arc_oil,0,LV_PART_INDICATOR);
    lv_obj_set_style_arc_width(arc_oil,14,LV_PART_MAIN);
    lv_obj_set_style_arc_width(arc_oil,14,LV_PART_INDICATOR);


    lv_obj_t *scale = lv_img_create(parent);//-4
    lv_img_set_src(scale,IMG_SCALE);
    //lv_img_set_zoom(hand_img, 350);
    lv_obj_align(scale,LV_ALIGN_TOP_LEFT,311,127);

    lv_obj_t* panel_bg_img_2 = lv_img_big_create(parent,panel_bg3_src,0,0,2,0);//-3
   //lv_obj_align(panel_bg_img_2,LV_ALIGN_TOP_LEFT,120,67);
   lv_obj_align(panel_bg_img_2,LV_ALIGN_CENTER,0,15);
    //
    lv_obj_t * speed_lab= lv_label_create(parent);//-2
    lv_obj_set_style_text_color(speed_lab,lv_color_hex(0xffffff),0);
    lv_obj_set_style_text_font(speed_lab,LV_FONT_BIG_NUMBER_44PX,0);
//    lv_obj_align(speed_lab,LV_ALIGN_TOP_LEFT, 185, 120);
    lv_label_set_text_fmt(speed_lab,"%d",0);
    lv_obj_align(speed_lab,LV_ALIGN_CENTER,0,0);

    lv_obj_t *oil_num = lv_img_create(parent);//-1
    lv_img_set_src(oil_num,IMG_OIL_NUM);
    //lv_img_set_zoom(hand_img, 350);
    lv_obj_align(oil_num,LV_ALIGN_TOP_LEFT,49,252);
	lv_obj_add_event_cb(speed_lab,page_del_cb,LV_EVENT_DELETE,NULL);
    refresh_timer = lv_timer_create(refresh_timer_cb,60,parent);

	lv_obj_add_event_cb(parent, page_event_handler, LV_EVENT_KEY, NULL);
}

